package com.eip.zd.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eip.zd.entity.Credit;
import com.eip.zd.entity.Cust;
import com.eip.zd.entity.Trade;
import com.eip.zd.exception.CustomException;
import com.eip.zd.mapper.CreditMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class CreditService {
    @Resource
    private CreditMapper creditMapper;
    @Resource
    private TradeService tradeService;
    @Resource
    private CustService custService;

    public List<Credit> getCreditList(String keyword) {
        List<Trade> tradeList = tradeService.getTradeList(null);
        List<Credit> creditList = creditMapper.getCreditList(keyword);
        creditList.stream().forEach(c -> {
            calc(c, tradeList);
        });
        return creditList;
    }

    public Credit getCreditByCustId(String custId) {
        List<Trade> tradeList = tradeService.getTradeListByCustId(custId);
        QueryWrapper<Credit> wrapper = new QueryWrapper<>();
        wrapper.eq("cust_id", custId);
        wrapper.eq("deleted", 0);
        Credit credit = creditMapper.selectOne(wrapper);
        if(credit == null) {
            credit = new Credit();
            credit.setAvailableCreditQuota(BigDecimal.valueOf(0));
            credit.setUsedCreditQuota(BigDecimal.valueOf(0));
            credit.setCreditQuota(BigDecimal.valueOf(0));
            credit.setCustId(custId);
        }
        Credit calc = calc(credit, tradeList);

        return calc;

    }

    public Credit calc(Credit credit, List<Trade> tradeList) {
        ArrayList<BigDecimal> bigDecimals = new ArrayList<>();
        Cust cust = custService.getCustByCustId(credit.getCustId());
        tradeList.stream().forEach(trade -> {
            if (credit.getCustId().equals(trade.getCustId())
                    && trade.getDeleted() == 0
                    && trade.getIfSettle() == 2
                    && cust.getBalance() == 1) {
                Trade calc = tradeService.calc(trade);
                BigDecimal unreceive = calc.getUnreceive();
                bigDecimals.add(unreceive);
            }
        });
        BigDecimal sum = bigDecimals.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
        credit.setUsedCreditQuota(sum);
        BigDecimal availableCreditQuota = credit.getCreditQuota().subtract(sum);
        credit.setAvailableCreditQuota(availableCreditQuota);
        return credit;
    }

    public void add(Credit credit) {
        if (StrUtil.hasEmpty(credit.getCustId())) {
            throw new CustomException(500, "客户编号不能为空");
        }
        QueryWrapper<Credit> wrapper = new QueryWrapper<>();
        wrapper.eq("cust_id", credit.getCustId());
        wrapper.eq("deleted", 0);
        Credit one = creditMapper.selectOne(wrapper);
        if (one != null) {
            throw new CustomException(500, "该客户授信已存在");
        }
        creditMapper.insert(credit);
    }

    public void update(Credit credit) {
        credit.setCreditQuota(credit.getCreditQuota().setScale(2, RoundingMode.HALF_UP));
        Credit his = creditMapper.selectById(credit.getId());
        if (his.getCreditQuota().equals(credit.getCreditQuota())) {
            creditMapper.updateById(credit);
        } else {
            creditMapper.deleteById(credit.getId());
            credit.setId(null);
            creditMapper.insert(credit);
        }
    }

    public void remove(Long id) {
        Credit credit = creditMapper.selectById(id);
        credit.setIfUse(2);
        creditMapper.updateById(credit);
        creditMapper.deleteById(id);
    }

    public void ifUse(Long id) {
        Credit credit = creditMapper.selectById(id);
        if (credit.getIfUse() == 1) {
            credit.setIfUse(2);
        } else {
            credit.setIfUse(1);
        }
        creditMapper.updateById(credit);
    }

    public List<Credit> getHisCreditList(String custId) {
        return creditMapper.getHisCreditList(custId);
    }
}
